﻿using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using LitJson;
using System;
using System.Collections.Generic;
using System.Xml;

public static class Assist
{
    public static int ToInt(this JsonData tj)
    {
        return Convert.ToInt32(tj.ToString());
    }
    public static float ToFloat(this JsonData tj)
    {
        return Convert.ToSingle(tj.ToString());
    }
    public static Vector3 ToVector3(this JsonData tj)
    {
        return new Vector3(tj["x"].ToFloat(), tj["y"].ToFloat(), tj["z"].ToFloat());
    }
    public static JsonData ToJsonData(this Vector3 tv)
    {
        var s = "{\"x\":" + tv.x + ",\"y\":" + tv.y + ",\"z\":" + tv.z + "}";
        return JsonMapper.ToObject(s);
    }
    public static JsonData ToJsonData(this List<Vector3> tl)
    {
        var tj = new JsonData();
        foreach (var i in tl)
        {
            tj.Add(i.ToJsonData());
        }
        return tj;
    }
    public static List<Vector3> ToListVector3(this JsonData tj)
    {
        var tl = new List<Vector3>();
        for (var i = 0; i < tj.Count; i++)
        {
            tl.Add(tj[i].ToVector3());
        }
        return tl;
    }
    public static List<string> GetKeys(this JsonData tj)
    {
        var kl = new List<string>();
        IDictionary dict = tj as IDictionary; //第一个子元素中包含多个子元素
        foreach (string key in dict.Keys)
        {
            kl.Add(key);
        }
        return kl;
    }
    public static string GetText(this GameObject tob)
    {
        var comp1 = tob.GetComponent<Text>();
        var comp2 = tob.GetComponent<InputField>();
        if (comp1 != null)
        {
            return comp1.text;
        }
        else
        {
            return comp2.text;
        }

    }
    public static void SetText(this GameObject tob, string s)
    {
        var comp1 = tob.GetComponent<Text>();
        var comp2 = tob.GetComponent<InputField>();
        if (comp1 != null)
        {
            comp1.text = s;
        }
        else
        {
            comp2.text = s;
        }
    }
    public static int ToInt(this string s)
    {
        return Convert.ToInt32(s);
    }
    public static void RemoveChild(this Transform t, string s)
    {
        UnityEngine.Object.Destroy(t.FindChild(s).gameObject);
    }
    public static XmlElement FindBy(this XmlNodeList nl, string s1, string s2)
    {
        foreach (XmlElement xe in nl)
        {
            if (xe.GetAttribute(s1) == s2)
            {
                return xe;
            }
        }
        return null;
    }
    public static bool IsExist(this JsonData jd, string s)
    {
        try
        {
            var a = jd[s];
            return true;
        }
        catch
        {
            return false;
        }
    }
    public static void AddTag(this GameObject obj, string tag)
    {
        if (!isHasTag(tag))
        {
            SerializedObject tagManager = new SerializedObject(obj);//序列化物体
            SerializedProperty it = tagManager.GetIterator();//序列化属性
            while (it.NextVisible(true))//下一属性的可见性
            {
                if (it.name == "m_TagString")
                {
                    
                    it.stringValue = tag;
                    tagManager.ApplyModifiedProperties();
                }
            }
        }
    }
    public static bool isHasTag(string tag)
    {
        for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.tags.Length; i++)
        {
            if (UnityEditorInternal.InternalEditorUtility.tags[i].Equals(tag))
                return true;
        }
        return false;
    }
}


